package yanglei.day0109.travel.dao.impl;


import yanglei.day0109.lottery.utils.BaseDao;
import yanglei.day0109.travel.dao.TourRouteDao;
import yanglei.day0109.travel.po.TourRoute;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: YangLei
 * @CreateTime: 2023-01-08  15:54
 * @Description: TODO
 */
public class TourRouteDaoImpl extends BaseDao implements TourRouteDao {


    @Override
    public List<TourRoute> queryTourRouteByType(Map<String, Object> map) {
        List<TourRoute> tourRoutes = new ArrayList<>();
        TourRoute tourRoute = null;
        String sql = "select tr_name,tr_type,tr_price,tr_phone,tr_user,create_time from tour_route";
        super.rs = super.executeQuery(sql);
        try {
            while (rs.next()){
                tourRoute = new TourRoute();
                tourRoute.setTrName(rs.getString("tr_name"));
                tourRoute.setTrType(rs.getInt("tr_type"));
                tourRoute.setTrPrice(rs.getInt("tr_price"));
                tourRoute.setTrPhone(rs.getString("tr_phone"));
                tourRoute.setTrUser(rs.getString("tr_user"));
                tourRoute.setCreateTime(rs.getString("create_time"));
                tourRoutes.add(tourRoute);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.closeAll(rs,pstmt,conn);
        }

        return tourRoutes;
    }

    @Override
    public int addTourRoute(TourRoute tourRoute) {
        int addRes = 0;
        StringBuffer sql = new StringBuffer();
        sql.append(" insert into tour_route ");
        sql.append("   (tr_id, tr_name, tr_type, tr_price, tr_phone, tr_user, create_time) ");
        sql.append(" values ");
        sql.append("   (travel_tour_route.nextval, ");
        sql.append(" ?,?,?,?,?,to_date(?, 'yyyy-mm-dd')) ");
        addRes = super.executeUpdate(sql.toString(),tourRoute.getTrName(),tourRoute.getTrType(),tourRoute.getTrPrice(),
                tourRoute.getTrPhone(),tourRoute.getTrUser(),tourRoute.getCreateTime());
        return addRes;
    }

    @Override
    public int deleteTourRoute(String trName) {
        StringBuffer sql = new StringBuffer();
        sql.append("delete from tour_route where tr_name = ?");
        return super.executeUpdate(sql.toString(),new Object[]{trName});
    }


}
